package org.kk.linked;

import org.kk.Common.ListNode;

/**
 * 删除排序链表中的重复元素
 */
public class DeleteDuplicates {
    /**
     * 示例
     * 输入 head = 1 -> 2 -> 2 - > 3
     * 输出 head = 1 -> 3
     */
    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null) return null;
        ListNode dummy  = new ListNode(-1);
        dummy.next = head;
        ListNode curr = dummy;
        while(curr.next != null && curr.next.next != null){
            if(curr.next.val == curr.next.next.val){
                int x = curr.next.val;
                while(curr.next != null && curr.next.val == x){
                    curr.next = curr.next.next;
                }
            }else{
                curr = curr.next;
            }
        }
        return dummy.next;
    }
}
